/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ideal.json;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Huyu
 */
public class JsonHelper {

    /**
     * 将Javabean转换为Map
     *
     * @param javaBean
     * @param map
     * @param key
     * @throws java.lang.IllegalAccessException
     */
    private void toMap(Object javaBean, Map map, String key) throws IllegalArgumentException, IllegalAccessException {
        Map result = new HashMap();

        Field[] fds = javaBean.getClass().getDeclaredFields();
        Field.setAccessible(fds, true);
        for (Field f : fds) {
            String Type = f.getType().toString();
            if (Type.startsWith("class") && Type.endsWith("String")) {
                result.put(f.getName(), f.get(javaBean));
            }
            if (Type.startsWith("class") && Type.endsWith("ArrayList")) {
                ArrayList<Object> list = (ArrayList<Object>) f.get(javaBean);
                if (!list.isEmpty()) {
                    for (int i = 0; i < list.size(); i++) {
                        String KeyName=f.getName() + String.valueOf(i);
                        toMap(list.get(i), result, KeyName);
                    }
                }
            }
//            System.out.println(f.getName());
//            System.out.println(f.getType());
//            System.out.println(f.get(javaBean));
        }
        map.put(key, result);
    }

    /**
     * 将Json对象转换成Map
     *
     * @param jsonString
     * @return Map对象
     * @throws JSONException
     */
    private Map toMap(String jsonString) throws JSONException {
        JSONObject jsonObject = new JSONObject(jsonString);
        Map result = new HashMap();
        Iterator iterator = jsonObject.keys();
        String key = null;
        String value = null;
        while (iterator.hasNext()) {
            key = (String) iterator.next();
            value = jsonObject.getString(key);
            result.put(key, value);
        }
        return result;

    }

    /**
     * 将Map转换成Javabean
     *
     * @param javabean javaBean
     * @param data Map数据
     * @return
     */
    private Object toJavaBean(Object javabean, Map data) {
        Method[] methods = javabean.getClass().getDeclaredMethods();
        for (Method method : methods) {

            try {
                if (method.getName().startsWith("set")) {
                    String field = method.getName();
                    field = field.substring(field.indexOf("set") + 3);
                    field = field.toLowerCase().charAt(0) + field.substring(1);
                    method.invoke(javabean, new Object[]{
                        data.get(field)
                    });
                }
            } catch (Exception e) {
            }

        }
        return javabean;
    }

    /**
     * JSONObject到JavaBean
     *
     * @param javabean
     * @param jsonString
     * @throws ParseException json解析异常
     * @throws JSONException
     */
    private void toJavaBean(Object javabean, String jsonString)
            throws ParseException, JSONException {
        JSONObject jsonObject = new JSONObject(jsonString);
        Map map = toMap(jsonObject.toString());
        toJavaBean(javabean, map);
    }

    /*
     *@param obj:转换的对象
     *@param key:外层键值
     */
    public String BuildJsonStr(Object obj, String key) throws JSONException, IllegalArgumentException, IllegalAccessException {
        Map result = new HashMap();
        toMap(obj, result, key);
        
        JSONObject jo1 =new JSONObject(result);
        
        return jo1.toString();
    }
}
